//
//  ViewController.m
//  练习
//
//  Created by Luck on 16/6/6.
//  Copyright © 2016年 yinjiao. All rights reserved.
//

#import "ViewController.h"
#import "Person2.h"
@interface ViewController () <UITableViewDataSource>
{
    
    NSArray *data;
    
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *url = [bundle URLForResource:@"tel2" withExtension:@""];
    NSString *text = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"读取数据：%@",text);
    
    NSArray *arr = [text componentsSeparatedByString:@"\n"];
    NSMutableArray *tmp = [NSMutableArray new];
    for(int i=0;i<arr.count;i++){
        
        NSString *item = arr[i];
        NSArray *p = [item componentsSeparatedByString:@","];
        Person2 *per = [Person2 new];
        per.name = p[0];
        per.tel2 = p[1];
        [tmp addObject:per];
    }
    
    data = tmp;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    Person2 *p = data[indexPath.row];
    cell.textLabel.text = p.name;
    cell.detailTextLabel.text = p.tel2;
    return cell;
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end